package com.neuedu.aop;

import com.neuedu.entity.User;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: MARK
 * @Date: 2019/7/25 10:01
 * @version: 1.0.0
 * @Description: 字符串分割代理类
 */
@Slf4j
public class LogHandler implements InvocationHandler {
    // TODO 1.声明一个目标对象
    private Object targetObject;

    // TODO 2.生成一个代理对象
    public Object newProxyInstance(Object targetObject){
        this.targetObject = targetObject;
        return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
                targetObject.getClass().getInterfaces(), this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object returnObj;
        start();
        returnObj = method.invoke(targetObject, args);
        end();
        return returnObj;
    }

    public void start(){
        log.info("调用"+targetObject.getClass().getName()+"之前日志输出");
    }

    public void end(){
        log.info("调用"+targetObject.getClass().getName()+"之后日志输出");
    }
}
